/**
 * Created by zlq on 2017/9/30.
 */


var page = 1;
var id;
var name,
    label,
    state,
    valueAddedServicesTypeValue,
    valueAddedServicesKindValue,
    warrantyPeriod,
    next,
    price,
    detailsUrl;


function doAjax(){
    $.ajax({
        url:'/crm/valueAddedServicesC/getList.json',
        dataType:'json',
        data:{
            token:localStorage.getItem('crmtoken'),
            valueAddedServicesKind:$('#valueAddedServicesKind1').val(),
            page:page,
        },
        type:'post',
        success: function (data) {
            if(data.code==-1){
                window.parent.location.href="/crm/login.jsp"
            }
            if(data.code==0){
                alert(data.msg);
            }
            if(data.code==1){
                $('#record').html(data.recordsTotal);
                $('#withdraw-record').empty();
                for(var i=0;i<data.datas.length;i++){

                    $('<tr class="gradeA odd">'+
                        '<a href=""><td class="detailsId"   >'+data.datas[i].id+'</td></a>'+
                        '<td class="sorting_1">'+data.datas[i].name+'</td>'+
                        '<td class="center ">'+data.datas[i].label+'</td>'+
                        '<td class="center ">'+data.datas[i].state+'</td>'+
                        '<td class="center ">'+data.datas[i].valueAddedServicesType +'</td>'+
                        '<td class=" center">'+data.datas[i].valueAddedServicesKind+'</td>'+
                        '<td class="center ">'+data.datas[i].warrantyPeriod+'个月</td>'+
                        '<td class="center ">'+data.datas[i].next+'</td>'+
                        '<td class=" center">'+data.datas[i].price+ '元</td>'+
                        '<td class=" center">'+data.datas[i].detailsUrl+'</td>'+
                        '<td class="mdvalueAddedServices"  data-toggle="modal" data-target="#myModal5" style="text-decoration: underline;cursor: pointer;" id="'+data.datas[i].id+'">修改</td>'+
                        '</tr>').appendTo($('#withdraw-record'));

                }
                $('.save').show();
                $(".pagination").createPage({
                    totalPage:data.totalPage,
                    currPage:data.pageNow,
                    pageCount:data.pageNow,
                    turndown:'true',
                    backFn:function(p){
                        page=p;
                        doAjax();
                    }
                });

                $('.mdvalueAddedServices').click(function(){
                    id = $(this).attr('id');
                    $('#addtitle').hide();
                    $('#save').hide();
                    $('#mdtitle').show();
                    $('#modification').show();
                    $('#label').attr("disabled",true);
                    getDetails(id);

                })


            }
        }
    })
}

$(function(){
    doAjax();
})


$('#valueAddedServicesType').click(function(){
    var  valueAddedServicesType;
    valueAddedServicesType=$('#valueAddedServicesType ').val();
    if(valueAddedServicesType==2){
        $('#next').attr("disabled",true);
    }else {
        $('#next').attr("disabled",false);
    }
})

$('#valueAddedServicesKind').click(function(){
    var  valueAddedServicesKind;
    valueAddedServicesKind=$('#valueAddedServicesKind ').val();
    var url='http://ddt.chinaddcat.com/m/phoneHtml/goodsService.html?service='+ valueAddedServicesKind;
    if(valueAddedServicesKind==null||valueAddedServicesKind==undefined||valueAddedServicesKind==""){
        $('#detailsUrl').val("");
    }else {
        $('#detailsUrl').val(url);
    }

})


//点击保存
$('#save').click(function(){
    $.ajax({
        url:'/crm/valueAddedServicesC/add.json',
        dataType:'json',
        data:{
            token:localStorage.getItem('crmtoken'),
            name:$('#name').val(),
            label:$('#label').val(),
            valueAddedServicesType:$('#valueAddedServicesType').val(),
            warrantyPeriod:$('#warrantyPeriod').val(),
            next:$('#next').val(),
            price:$('#price').val(),
            detailsUrl:$('#detailsUrl').val(),
            valueAddedServicesKind:$('#valueAddedServicesKind').val(),
            state:$('#state').val(),

        },
        type:'post',
        success:function(data){
            if(data.code==0){
                alert(data.msg)
                return;
            }
            if(data.code==-1){
                window.parent.location.href="/crm/login.jsp"
            }
            if(data.code==1){
                alert('创建成功');
                $('#myModal5').removeClass('fade in');
                $('#myModal5').addClass('fade');
                $('#myModal5').attr('aria-hidden',true);
                $('#myModal5').css('display','none');
                $('.modal-backdrop').css('display','none');
                $('#myModal5 input').val('');
                page=1;
                doAjax();
            }
        }
    })
})



function getDetails(id){
    $.ajax({
        url:'/crm/valueAddedServicesC/getDetails.json',
        dataType:'json',
        data:{
            token:localStorage.getItem('crmtoken'),
            id:id,
        },
        type:'post',
        success:function(data){
            if(data.code==0){
                alert(data.msg)
                return;
            }
            if(data.code==-1){
                window.parent.location.href="/crm/login.jsp"
            }
            if(data.code==1){

                $('#name').val(data.datas.name);
                $('#label').val(data.datas.label);
                $('#valueAddedServicesType').val(data.datas.valueAddedServicesTypeValue);
                $('#warrantyPeriod').val(data.datas.warrantyPeriod);
                $('#next').val(data.datas.next);
                $('#price').val(data.datas.price);
                $('#detailsUrl').val(data.datas.detailsUrl);
                $('#valueAddedServicesKind').val(data.datas.valueAddedServicesKindValue);
                $('#state').val(data.datas.stateValue);
            }
        }
    })
}

//点击添加
$('#add').click(function(){
    $('#addtitle').show();
    $('#save').show();
    $('#mdtitle').hide();
    $('#modification').hide();
    $('#label').attr("disabled",false);

})


//点击保存
$('#modification').click(function(){
    $.ajax({
        url:'/crm/valueAddedServicesC/modification.json',
        dataType:'json',
        data:{
            token:localStorage.getItem('crmtoken'),
            id:id,
            name:$('#name').val(),
            valueAddedServicesType:$('#valueAddedServicesType').val(),
            warrantyPeriod:$('#warrantyPeriod').val(),
            next:$('#next').val(),
            price:$('#price').val(),
            detailsUrl:$('#detailsUrl').val(),
            valueAddedServicesKind:$('#valueAddedServicesKind').val(),
            state:$('#state').val(),

        },
        type:'post',
        success:function(data){
            if(data.code==0){
                alert(data.msg)
                return;
            }
            if(data.code==-1){
                window.parent.location.href="/crm/login.jsp"
            }
            if(data.code==1){
                alert('修改成功');
                $('#myModal5').removeClass('fade in');
                $('#myModal5').addClass('fade');
                $('#myModal5').attr('aria-hidden',true);
                $('#myModal5').css('display','none');
                $('.modal-backdrop').css('display','none');
                $('#myModal5 input').val('');
                page=1;
                doAjax();
            }
        }
    })
})


//查询
$('#refer').click(function(){
    page=1;
    doAjax();
})